package com.js.data.dtmg.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.data.common.api.CommonResult;
import com.js.data.dtmg.dto.DtTaskCronDto;
import com.js.data.dtmg.dto.DtTaskDto;
import com.js.data.dtmg.dto.DtTaskQuery;
import com.js.data.dtmg.entity.DtTask;
import com.js.data.dtmg.service.DtTaskService;
import com.js.data.dtmg.vo.DtTaskVO;
import com.js.data.system.annotation.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author hh
 * @since 2024-07-17
 */
@RestController
@RequestMapping("/dtmg/dtTask")
public class DtTaskController {

    @Resource
    private DtTaskService service;

    /**
     * 用户分页
     *
     * @param query 查询条件
     * @return IPage<UserPageVO>
     */
    @GetMapping(value = "pageList")
    public CommonResult<IPage<DtTaskVO>> pageList(@ModelAttribute DtTaskQuery query) {
        return CommonResult.success(service.pageList(query));
    }
    /**
     * 新增
     *
     * @param dto 新增
     * @return void
     */
    @Log(value = "新增任务")
    @PostMapping(value = "/save")
    public CommonResult<Void> save(@Validated @RequestBody DtTaskDto dto) {
        service.save(dto);
        return CommonResult.success();
    }
    /**
     * 查询任务详情
     *
     * @param id 任务id
     * @return DtDatasourceVO
     */
    @GetMapping(value = "/detail/{id}")
    public CommonResult<DtTaskVO> detail(@PathVariable Long id) {
        return CommonResult.success(service.detail(id));
    }
    /**
     * 删除任务
     *
     * @param id 任务id
     * @return void
     */
    @Log(value = "删除任务")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return CommonResult.success();
    }
    /**
     * 新增
     *
     * @param dto 新增
     * @return void
     */
    @Log(value = "处理任务")
    @PostMapping(value = "/doTask")
    public CommonResult<Map<String,Object>> doTask(@Validated @RequestBody DtTaskDto dto) {
        DtTask task = new DtTask();
        BeanUtils.copyProperties(dto,task);
        return CommonResult.success(service.doTask(task,""));
    }


    /**
     * 新增
     *
     * @param dto 新增
     * @return void
     */
    @Log(value = "新增定时任务")
    @PostMapping(value = "/saveCron")
    public CommonResult<Void> saveCron(@Validated @RequestBody DtTaskCronDto dto) {
        service.saveCron(dto);
        return CommonResult.success();
    }

}

